package com.missilike.api.controller;

import com.missilike.biz.IMilContentCollectionService;
import com.missilike.core.result.BaseResultCode;
import com.missilike.core.result.CommonResult;
import com.missilike.core.utils.ResultUtils;
import com.missilike.domain.response.CollectionResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/collection")
public class CollectionController {

    private final IMilContentCollectionService collectionService;

    public CollectionController(IMilContentCollectionService collectionService) {
        this.collectionService = collectionService;
    }

    @ApiOperation(value = "getUserCollectionList", notes = "获取收藏列表")
    @RequestMapping(value = "/getUserCollectionList", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult getUserCollectionList(@RequestParam @ApiParam(value = "用户id") String accountId,
                                              @RequestParam(defaultValue = "20") Integer pageSize,
                                              @RequestParam(defaultValue = "0") Long lastId,
                                              @RequestParam(value = "type",defaultValue = "1")@ApiParam(value = "1:非长文,2:长文 ") Integer type) {
        CollectionResponse response = collectionService.getUserCollectionList(accountId,pageSize,lastId,type);
        return ResultUtils.ok(response);
    }

    @ApiOperation(value = "getUserCollectionList", notes = "获取被收藏列表")
    @RequestMapping(value = "/getUserBeCollectedList", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult getUserBeCollectedList(@RequestParam @ApiParam(value = "用户id") String accountId,
                                              @RequestParam(defaultValue = "20") Integer pageSize,
                                              @RequestParam(defaultValue = "0") Long lastId,
                                              @RequestParam(value = "type",defaultValue = "1")@ApiParam(value = "1:非长文,2:长文 ") Integer type) {
        CollectionResponse response = collectionService.getUserBeCollectedList(accountId,pageSize,lastId,type);
        return ResultUtils.ok(response);
    }

    @ApiOperation(value = "add", notes = "收藏")
    @RequestMapping(value = "/add", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult add(@RequestParam @ApiParam(value = "用户id") String accountId, @RequestParam @ApiParam(value = "收藏内容id") Long contentId,
                            @RequestParam @ApiParam(value = "被收藏内容作者Id") String collectionAccountId) {

        if(StringUtils.isBlank(accountId) || contentId == null || StringUtils.isBlank(collectionAccountId)){
            return ResultUtils.error(BaseResultCode.PARAMETER_ERROR.getCode(),"参数为空");
        }

        collectionService.add(accountId,contentId,collectionAccountId);
        return ResultUtils.ok();
    }

    @ApiOperation(value = "del", notes = "取消收藏")
    @RequestMapping(value = "/del", method = RequestMethod.GET, produces = "application/json")
    @ResponseBody
    public CommonResult add(@RequestParam @ApiParam(value = "收藏id") Long id) {
        if(id == null ){
            return ResultUtils.error(BaseResultCode.PARAMETER_ERROR.getCode(),"参数为空");
        }
        collectionService.removeById(id);
        return ResultUtils.ok();
    }

}
